<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>常量表达式</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/constant_expression","wgTitle":"cpp/language/constant expression","wgCurRevisionId":64493,"wgArticleId":8207,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Pages with unreviewed CWG DR marker","Todo with reason","Pages with unreviewed LWG DR marker"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/constant_expression","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_constant_expression skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">常量表达式</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="expressions.html" title="cpp/language/expressions"> 表达式</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> 概述 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="value_category.html" title="cpp/language/value category"> 值类别</a>（左值 lvalue、右值 rvalue、亡值 xvalue）</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="eval_order.html" title="cpp/language/eval order"> 求值顺序</a>（序列点）</td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 常量表达式</strong></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="expressions.html#.E4.B8.8D.E6.B1.82.E5.80.BC.E8.A1.A8.E8.BE.BE.E5.BC.8F" title="cpp/language/expressions"> 不求值表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="expressions.html#.E5.88.9D.E7.AD.89.E8.A1.A8.E8.BE.BE.E5.BC.8F" title="cpp/language/expressions"> 初等表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lambda.html" title="cpp/language/lambda">lambda 表达式</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 字面量 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="integer_literal.html" title="cpp/language/integer literal">整数字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="floating_literal.html" title="cpp/language/floating literal">浮点字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="bool_literal.html" title="cpp/language/bool literal">布尔字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="character_literal.html" title="cpp/language/character literal">字符字面量</a>，包含<a href="escape.html" title="cpp/language/escape">转义序列</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="string_literal.html" title="cpp/language/string literal">字符串字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="nullptr.html" title="cpp/language/nullptr">空指针字面量</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="user_literal.html" title="cpp/language/user literal">用户定义字面量</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 运算符 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_assignment.html" title="cpp/language/operator assignment"> 赋值运算符</a>:
<code>a=b</code>, <code>a+=b</code>, <code>a-=b</code>, <code>a*=b</code>, <code>a/=b</code>, <code>a%=b</code>, <code>a&amp;=b</code>, <code>a|=b</code>, <code>a^=b</code>, <code>a&lt;&lt;=b</code>, <code>a&gt;&gt;=b</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_incdec.html" title="cpp/language/operator incdec"> 自增与自减</a>: <code>++a</code>, <code>--a</code>, <code>a++</code>, <code>a--</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_arithmetic.html" title="cpp/language/operator arithmetic"> 算术运算符</a>:
<code>+a</code>, <code>-a</code>, <code>a+b</code>, <code>a-b</code>, <code>a*b</code>, <code>a/b</code>, <code>a%b</code>, <code>~a</code>, <code>a&amp;b</code>, <code>a|b</code>, <code>a^b</code>, <code>a&lt;&lt;b</code>, <code>a&gt;&gt;b</code> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_logical.html" title="cpp/language/operator logical"> 逻辑运算符</a>: <code>a||b</code>, <code>a&amp;&amp;b</code>, <code>!a</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_comparison.html" title="cpp/language/operator comparison"> 比较运算符</a>: <code>a==b</code>, <code>a!=b</code>, <code>a&lt;b</code>, <code>a&gt;b</code>, <code>a&lt;=b</code>, <code>a&gt;=b</code>, <code>a&lt;=&gt;b</code><span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_member_access.html" title="cpp/language/operator member access"> 成员访问运算符</a>: <code>a[b]</code>, <code>*a</code>, <code>&amp;a</code>, <code>a-&gt;b</code>, <code>a.b</code>, <code>a-&gt;*b</code>, <code>a.*b</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_other.html" title="cpp/language/operator other"> 其他运算符</a>: <code>a(...)</code>, <code>a,b</code>, <code>a?b:c</code> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="default_comparisons.html" title="cpp/language/default comparisons">默认比较</a><span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_alternative.html" title="cpp/language/operator alternative">运算符的代用表示</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_precedence.html" title="cpp/language/operator precedence">优先级和结合性</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="fold.html" title="cpp/language/fold">折叠表达式</a><span class="t-mark-rev t-since-cxx17">(C++17)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="new.html" title="cpp/language/new">new 表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="delete.html" title="cpp/language/delete">delete 表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="throw.html" title="cpp/language/throw">throw 表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="alignof.html" title="cpp/language/alignof">alignof</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="sizeof.html" title="cpp/language/sizeof">sizeof</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="sizeof....html" title="cpp/language/sizeof...">sizeof...</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="typeid.html" title="cpp/language/typeid">typeid</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="noexcept.html" title="cpp/language/noexcept">noexcept</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operators.html" title="cpp/language/operators">运算符重载</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 类型转换 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="implicit_cast.html" title="cpp/language/implicit conversion">隐式转换</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="const_cast.html" title="cpp/language/const cast">const_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_cast.html" title="cpp/language/static cast">static_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reinterpret_cast.html" title="cpp/language/reinterpret cast">reinterpret_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="dynamic_cast.html" title="cpp/language/dynamic cast">dynamic_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit_cast.html" title="cpp/language/explicit cast">显式转换</a> <code>(T)a</code>, <code>T(a)</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="cast_operator.html" title="cpp/language/cast operator">用户定义转换</a></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>定义能在编译时求值的<a href="expressions.html" title="cpp/language/expressions">表达式</a>。
</p><p>这种表达式能用做非类型模板实参、数组大小，并用于其他要求常量表达式的语境，例如
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
<a href="../container/array.html"><span class="kw1268">std::<span class="me2">array</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span>, n<span class="sy1">&gt;</span> a1<span class="sy4">;</span> <span class="co1">// 错误：n 不是常量表达式</span>
<span class="kw4">const</span> <span class="kw4">int</span> cn <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
<a href="../container/array.html"><span class="kw1268">std::<span class="me2">array</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span>, cn<span class="sy1">&gt;</span> a2<span class="sy4">;</span> <span class="co1">// OK：cn 是常量表达式</span></pre></div></div>
<h3><span class="mw-headline" id=".E6.A0.B8.E5.BF.83.E5.B8.B8.E9.87.8F.E8.A1.A8.E8.BE.BE.E5.BC.8F">核心常量表达式</span></h3>
<p><i>核心（core）常量表达式</i>是求值过程中<i>不会</i>对下列任一者求值的表达式：
</p>
<ol>
<li><a href="this.html" title="cpp/language/this"><code>this</code></a> 指针，但作为该表达式的一部分求值的 <code>constexpr</code> 函数或 <code>constexpr</code> 构造函数之中的则不算
</li><li>调用未声明为 <a href="constexpr.html" title="cpp/language/constexpr">constexpr</a> 的函数（或构造函数）的函数调用表达式
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">constexpr</span> <span class="kw4">int</span> n <span class="sy1">=</span> <a href="../types/numeric_limits.html"><span class="kw344">std::<span class="me2">numeric_limits</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">max</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK：max() 是 constexpr</span>
<span class="kw4">constexpr</span> <span class="kw4">int</span> m <span class="sy1">=</span> <a href="../chrono/c/time.html"><span class="kw1011">std::<span class="me2">time</span></span></a><span class="br0">(</span><a href="../types/NULL.html"><span class="kw103">NULL</span></a><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：std::time() 非 constexpr</span></pre></div></div>
</li><li>对声明为 <code>constexpr</code> 但未定义的函数进行的函数调用
</li><li>对 <code>constexpr</code> 函数/构造函数模板实例化进行的函数调用，其中该实例化无法满足 <a href="constexpr.html" title="cpp/language/constexpr">constexpr 函数/构造函数</a>的要求。
</li><li> <span class="t-mark-rev t-since-cxx20">(C++20 起)</span> 对 <code>constexpr</code> 虚函数的函数调用，调用所用的对象不<i>可用于常量表达式</i>（见后述）且其生命期始于此表达式之外。
</li><li>会超出实现定义限制的表达式
</li><li>求值导致任何形式的核心语言未定义行为（包含有符号整数溢出、除以零、数组边界外的指针算术等）的表达式。是否检测标准库的未定义行为是未指明的。
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">constexpr</span> <span class="kw4">double</span> d1 <span class="sy1">=</span> <span class="nu16">2.0</span><span class="sy2">/</span><span class="nu16">1.0</span><span class="sy4">;</span> <span class="co1">// OK</span>
<span class="kw4">constexpr</span> <span class="kw4">double</span> d2 <span class="sy1">=</span> <span class="nu16">2.0</span><span class="sy2">/</span><span class="nu16">0.0</span><span class="sy4">;</span> <span class="co1">// 错误：未定义</span>
<span class="kw4">constexpr</span> <span class="kw4">int</span> n <span class="sy1">=</span> <a href="../types/numeric_limits.html"><span class="kw344">std::<span class="me2">numeric_limits</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">max</span><span class="br0">(</span><span class="br0">)</span> <span class="sy2">+</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// 错误：溢出</span>
<span class="kw4">int</span> x, y, z<span class="br0">[</span><span class="nu0">30</span><span class="br0">]</span><span class="sy4">;</span>
<span class="kw4">constexpr</span> <span class="kw4">auto</span> e1 <span class="sy1">=</span> <span class="sy3">&amp;</span>y <span class="sy2">-</span> <span class="sy3">&amp;</span>x<span class="sy4">;</span> <span class="co1">// 错误：未定义</span>
<span class="kw4">constexpr</span> <span class="kw4">auto</span> e2 <span class="sy1">=</span> <span class="sy3">&amp;</span>z<span class="br0">[</span><span class="nu0">20</span><span class="br0">]</span> <span class="sy2">-</span> <span class="sy3">&amp;</span>z<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="sy4">;</span> <span class="co1">// OK</span>
<span class="kw4">constexpr</span> <a href="../utility/bitset.html"><span class="kw1027">std::<span class="me2">bitset</span></span></a><span class="sy1">&lt;</span><span class="nu0">2</span><span class="sy1">&gt;</span> a<span class="sy4">;</span> 
<span class="kw4">constexpr</span> <span class="kw4">bool</span> b <span class="sy1">=</span> a<span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span><span class="sy4">;</span> <span class="co1">// UB，但是否检测未指定</span></pre></div></div>
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx17"> <span><span class="t-mark-rev t-until-cxx17">(C++17 前)</span><a href="lambda.html" title="cpp/language/lambda">lambda 表达式</a></span>
</li><li>左值到右值<a href="implicit_cast.html" title="cpp/language/implicit conversion">隐式转换</a>，除非……
<ol type="a">
<li>应用到指代<i>可用于常量表达式</i>（见后述）的对象的非 volatile 泛左值，
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="kw4">const</span> <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> tabsize <span class="sy1">=</span> <span class="nu0">50</span><span class="sy4">;</span>
    <span class="kw4">int</span> tab<span class="br0">[</span>tabsize<span class="br0">]</span><span class="sy4">;</span> <span class="co1">// OK ： tabsize 是常量表达式</span>
                      <span class="co1">// 因为 tabsize 可用于常量表达式</span>
                      <span class="co1">// 因为它有 const 限定的整数类型，且其初始化器是常量初始化器</span>
 
    <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> n <span class="sy1">=</span> <span class="nu0">50</span><span class="sy4">;</span>
    <span class="kw4">const</span> <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> sz <span class="sy1">=</span> n<span class="sy4">;</span>
    <span class="kw4">int</span> tab2<span class="br0">[</span>sz<span class="br0">]</span><span class="sy4">;</span> <span class="co1">// 错误： sz 不是常量表达式</span>
                  <span class="co1">// 因为 sz 不可用于常量表达式</span>
                  <span class="co1">// 因为其初始化器不是常量初始化器</span>
<span class="br0">}</span>
<span class="br0">}</span></pre></div></div>
</li><li>或应用到拥有字面类型，并指代生命期始于此表达式的求值之内的非 volatile 对象
</li></ol>
</li><li>对 <a href="union.html" title="cpp/language/union">union</a> 的不活跃成员或其子对象实施的左值到右值<a href="implicit_cast.html" title="cpp/language/implicit conversion">隐式转换</a>或修改操作（即使它与活跃成员共享公共起始序列也是如此）
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span> 应用到拥有<a href="default_initialization.html" title="cpp/language/default initialization">不确定值</a>的对象的左值到右值隐式转换</span>
</li><li>对活跃成员（若存在）为 mutable 的 union 调用隐式定义的复制/移动构造函数或复制/移动赋值运算符，除非该 union 对象的生命期始于此表达式的求值之内
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx17 t-until-cxx20"> <span><span class="t-mark-rev t-since-cxx17">(C++17 起)</span> <span class="t-mark-rev t-until-cxx20">(C++20 前)</span>会更改 union 的活跃成员的赋值表达式，或对重载的赋值运算符的调用</span>
</li><li>指代引用类型的变量或数据成员的 <span class="t-spar">标识表达式</span>，除非该引用<i>可用于常量表达式</i>，或其生命始于此表达式的求值之内
</li><li>从 <i>cv</i> <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">void</span><span class="sy2">*</span></span></span> 转换到任何对象指针类型
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx20"> <span><span class="t-mark-rev t-until-cxx20">(C++20 前)</span><a href="dynamic_cast.html" title="cpp/language/dynamic cast"><code>dynamic_cast</code></a></span>
</li><li><a href="reinterpret_cast.html" title="cpp/language/reinterpret cast"><code>reinterpret_cast</code></a>
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx20"> <span><span class="t-mark-rev t-until-cxx20">(C++20 前)</span>伪析构函数调用</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx14"> <span><span class="t-mark-rev t-until-cxx14">(C++14 前)</span>自增或自减运算符</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx14"> <div>
<p><span class="t-mark-rev t-since-cxx14">(C++14 起)</span> 修改对象，除非该对象拥有非 volatile 字面类型，且其生命期始于此表达式的求值之内
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">constexpr</span> <span class="kw4">int</span> incr<span class="br0">(</span><span class="kw4">int</span><span class="sy3">&amp;</span> n<span class="br0">)</span> <span class="br0">{</span>
  <span class="kw1">return</span> <span class="sy2">++</span>n<span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw4">constexpr</span> <span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">int</span> k<span class="br0">)</span> <span class="br0">{</span>
  <span class="kw4">constexpr</span> <span class="kw4">int</span> x <span class="sy1">=</span> incr<span class="br0">(</span>k<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：incr(k) 不是核心常量表达式</span>
                             <span class="co1">// 因为 k 的生命期始于表达式 incr(k) 之外</span>
  <span class="kw1">return</span> x<span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw4">constexpr</span> <span class="kw4">int</span> h<span class="br0">(</span><span class="kw4">int</span> k<span class="br0">)</span> <span class="br0">{</span>
  <span class="kw4">int</span> x <span class="sy1">=</span> incr<span class="br0">(</span>k<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK：不要求 x 以核心常量表达式初始化</span>
  <span class="kw1">return</span> x<span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw4">constexpr</span> <span class="kw4">int</span> y <span class="sy1">=</span> h<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK：以值 2 初始化 y</span>
                        <span class="co1">// h(1) 是核心常量表达式</span>
                        <span class="co1">// 因为 k 的生命期始于表达式 h(1) 之内</span></pre></div></div>
</div>
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx20"> <span><span class="t-mark-rev t-until-cxx20">(C++20 前)</span>应用到多态类型泛左值的 <a href="typeid.html" title="cpp/language/typeid"><code>typeid</code></a> 表达式</span>
</li><li> <a href="new.html" title="cpp/language/new">new 表达式</a><span class="t-rev-inl t-since-cxx20"><span>，除非选择的<a href="../memory/new/operator_new.html" title="cpp/memory/new/operator new">分配函数</a>是全局可替换分配函数且此表达式的求值内解分配其所分配的存储</span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>
</li><li> <a href="delete.html" title="cpp/language/delete">delete 表达式</a><span class="t-rev-inl t-since-cxx20"><span>，除非它解分配于此表达式的求值内分配的存储区域</span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span>调用 <span class="t-lc"><a href="../memory/allocator/allocate.html" title="cpp/memory/allocator/allocate">std::allocator&lt;T&gt;::allocate</a></span> ，除非于此表达式的求值内解分配其所分配的存储</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span>调用 <span class="t-lc"><a href="../memory/allocator/deallocate.html" title="cpp/memory/allocator/deallocate">std::allocator&lt;T&gt;::deallocate</a></span> ，除非它解分配于此表达式的求值内分配的存储区域</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span><a href="coroutines.html#co_await" title="cpp/language/coroutines">await 表达式</a>或 <a href="coroutines.html#co_yield" title="cpp/language/coroutines">yield 表达式</a></span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span>结果未指定<a href="operator_comparison.html#.E4.B8.89.E8.B7.AF.E6.AF.94.E8.BE.83" title="cpp/language/operator comparison">三路比较</a></span>
</li><li>结果未指明的相等或关系运算符
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx14"> <span><span class="t-mark-rev t-until-cxx14">(C++14 前)</span>赋值或复合赋值运算符</span>
</li><li>throw 表达式
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span> <a href="asm.html" title="cpp/language/asm">汇编声明</a></span>
</li><li> 宏 <span class="t-lc"><a href="../utility/variadic/va_arg.html" title="cpp/utility/variadic/va arg">va_arg</a></span> 的调用，是否能求值宏 <span class="t-lc"><a href="../utility/variadic/va_start.html" title="cpp/utility/variadic/va start">va_start</a></span> 是未指定的
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span>会抛出异常的 <a href="dynamic_cast.html" title="cpp/language/dynamic cast"><code>dynamic_cast</code></a> 或 <a href="typeid.html" title="cpp/language/typeid"><code>typeid</code></a> 表达式</span>
</li><li>lambda 表达式中，提及 <code>this</code> 或提及定义于该 lambda 之外的变量，若它是一次 ODR 式使用
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
  <span class="kw4">const</span> <span class="kw4">int</span> n<span class="sy1">=</span><span class="nu0">0</span><span class="sy4">;</span>
  <span class="kw4">constexpr</span> <span class="kw4">int</span> j<span class="sy1">=</span><span class="sy2">*</span><span class="sy3">&amp;</span>n<span class="sy4">;</span> <span class="co1">// OK：lambda 表达式之外</span>
  <span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span><span class="br0">{</span> <span class="kw4">constexpr</span> <span class="kw4">int</span> i<span class="sy1">=</span>n<span class="sy4">;</span>  <span class="co1">// OK：'n' 未被 ODR 式使用且未在此处被俘获。</span>
       <span class="kw4">constexpr</span> <span class="kw4">int</span> j<span class="sy1">=</span><span class="sy2">*</span><span class="sy3">&amp;</span>n<span class="sy4">;</span><span class="co1">// 非良构：'&amp;n' ODR 式使用了 'n'。</span>
     <span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<p>注意，若 ODR 式使用发生于对闭包的函数调用中，则它不涉指 <code>this</code> 或外围变量，因为它所访问的是该闭包的数据成员
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// OK：'v' 与 'm' 被 ODR 式使用，但未出现于嵌套于 lambda 内的常量表达式中</span>
<span class="kw4">auto</span> monad <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> v<span class="br0">)</span><span class="br0">{</span><span class="kw1">return</span> <span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span><span class="br0">{</span><span class="kw1">return</span> v<span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">auto</span> bind <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> m<span class="br0">)</span><span class="br0">{</span><span class="kw1">return</span> <span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> fvm<span class="br0">)</span><span class="br0">{</span><span class="kw1">return</span> fvm<span class="br0">(</span>m<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span>
<span class="co1">// 在常量表达式求值中，创建对自动对象的俘获是 OK 的。</span>
static_assert<span class="br0">(</span>bind<span class="br0">(</span>monad<span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="br0">)</span><span class="br0">(</span>monad<span class="br0">)</span><span class="br0">(</span><span class="br0">)</span> <span class="sy1">==</span> monad<span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table>
</li></ol>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：需要更多小示例和更少标准用语 </td></tr></table>
<p>注意：<i>核心常量表达式</i>本身并无任何直接的语义含义：表达式必须为下列子集之一，才可以在特定语境中使用：
</p>
<h4><span class="mw-headline" id=".E5.8F.AF.E7.94.A8.E4.BA.8E.E5.B8.B8.E9.87.8F.E8.A1.A8.E8.BE.BE.E5.BC.8F">可用于常量表达式</span></h4>
<p>上述列表中，变量<i>可用于常量表达式</i>，若它是
</p>
<ul><li> constexpr 变量，或
</li><li> 拥有下列类型的<a href="constant_initialization.html" title="cpp/language/constant initialization">常量初始化的</a>变量
</li></ul>
<dl><dd><ul><li> 引用类型，或
</li><li> const 限定的整数或枚举类型。
</li></ul>
</dd></dl>
<p>对象或引用<i>可用于常量表达式</i>，若它是
</p>
<ul><li> <i>可用于常量表达式</i>的变量，或
</li><li> <span class="t-mark-rev t-since-cxx20">(C++20 起)</span> <a href="template_parameters.html#.E9.9D.9E.E7.B1.BB.E5.9E.8B.E6.A8.A1.E6.9D.BF.E5.BD.A2.E5.8F.82" title="cpp/language/template parameters">模板形参对象</a>，或
</li><li> 字符串字面量对象，或
</li><li> 以上任一者的非 mutable 子对象或引用成员，或
</li><li> 非 volatile 的 const 限定整数类或枚举类型的，以常量表达式初始化的完整临时对象。
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">const</span> <a href="../iterator/size.html"><span class="kw684">std::<span class="me2">size</span></span></a> sz <span class="sy1">=</span> <span class="nu0">10</span><span class="sy4">;</span> <span class="co1">// sz 可用于常量表达式</span></pre></div></div>
<h3><span class="mw-headline" id=".E6.95.B4.E5.9E.8B.E5.B8.B8.E9.87.8F.E8.A1.A8.E8.BE.BE.E5.BC.8F">整型常量表达式</span></h3>
<p><i>整型常量表达式</i>是隐式转换成纯右值的整型或无作用域枚举类型的表达式，其中被转换的表达式是<i>核心常量表达式</i>。若将类类型表达式用在期待整型常量表达式之处，则表达式将被<a href="implicit_cast.html" title="cpp/language/implicit conversion">按语境隐式转换</a>成整型或无作用域枚举类型。
</p><p>下列语境要求<i>整型常量表达式</i>：
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx14"><td>
<ul><li> <a href="array.html" title="cpp/language/array">数组边界</a>
</li><li> <a href="new.html" title="cpp/language/new">new 表达式</a>中首个以外的维度
</li></ul>
</td>
<td><span class="t-mark-rev t-until-cxx14">(C++14 前)</span></td></tr>
</table>
<ul><li> <a href="bit_field.html" title="cpp/language/bit field">位域长度</a>
</li><li> 底层类型未固定时的枚举初始化器
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx14"><td>
<ul><li> 空指针常量
</li></ul>
</td>
<td><span class="t-mark-rev t-until-cxx14">(C++14 前)</span></td></tr>
</table>
<ul><li> <a href="alignas.html" title="cpp/language/alignas">对齐</a>。
</li></ul>
<h3><span class="mw-headline" id=".E7.BB.8F.E8.BD.AC.E6.8D.A2.E7.9A.84.E5.B8.B8.E9.87.8F.E8.A1.A8.E8.BE.BE.E5.BC.8F">经转换的常量表达式</span></h3>
<p><code>T</code> 类型的<i>经转换的（converted）常量表达式</i>是<a href="implicit_cast.html" title="cpp/language/implicit conversion">隐式转换</a>到 T 类型的表达式，其中被转换后表达式是常量表达式，且隐式转换序列只含有：
</p>
<dl><dd><ul><li> constexpr 用户定义转换（故类能用于期待整数类型之处）
</li><li> 左值到右值转换
</li><li> 整型提升
</li><li> 非窄化整型转换
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<dl><dd><ul><li> 数组到指针转换
</li><li> 函数到指针转换
</li><li> 函数指针转换（指向 noexcept 函数的指针到指向函数的指针）
</li><li> 限定性转换
</li><li> 源自 <span class="t-lc"><a href="../types/nullptr_t.html" title="cpp/types/nullptr t">std::nullptr_t</a></span> 的空指针转换
</li><li> 源自 <span class="t-lc"><a href="../types/nullptr_t.html" title="cpp/types/nullptr t">std::nullptr_t</a></span> 的空成员指针转换
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table>
<dl><dd><ul><li> 而若发生任何<a href="reference_initialization.html" title="cpp/language/reference initialization">引用绑定</a>，则它是直接绑定（非构造临时对象者）
</li></ul>
</dd></dl>
<p>下列语境要求<i>经转换的常量表达式</i>：
</p>
<ul><li> <a href="switch.html" title="cpp/language/switch">case 表达式</a>
</li><li> 底层类型已固定时的<a href="enum.html" title="cpp/language/enum">枚举项初始化器</a>
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<ul><li> <a href="array.html" title="cpp/language/array">数组边界</a>
</li><li> <a href="new.html" title="cpp/language/new">new 表达式</a>中，第一维以外的维度
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></td></tr>
</table>
<ul><li> <span class="t-rev-inl t-until-cxx17"><span>整型与枚举</span> <span><span class="t-mark-rev t-until-cxx17">(C++17 前)</span></span></span>非类型<a href="template_parameters.html" title="cpp/language/template parameters">模板实参</a>。
</li></ul>
<p><i>按语境转换（contextually converted）的 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span></span></span> 类型常量表达式</i>是<a href="implicit_cast.html#.E6.8C.89.E8.AF.AD.E5.A2.83.E8.BD.AC.E6.8D.A2" title="cpp/language/implicit conversion">按语境转换到 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span></span></span></a> 的表达式，其中转换后的表达式是常量表达式，且转换序列只含上述转换。
</p><p>下列语境要求<i>按语境转换的 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span></span></span> 类型常量表达式</i>：
</p>
<ul><li> <a href="noexcept_spec.html" title="cpp/language/noexcept spec"><span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">noexcept</span></span></span> 规定</a>
</li><li> <a href="static_assert.html" title="cpp/language/static assert">静态断言（<code>static_assert</code>）声明</a>
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<ul><li> <a href="if.html#constexpr_if" title="cpp/language/if">constexpr if 语句</a>
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<ul><li> <a href="explicit.html" title="cpp/language/explicit">条件性 <code>explicit</code> 说明符</a>
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E5.B8.B8.E9.87.8F.E8.A1.A8.E8.BE.BE.E5.BC.8F">常量表达式</span></h3>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<p><i>常量表达式（constant expression）</i>是
</p>
<ul><li> 指代下列之一的泛左值<i>核心常量表达式</i>
</li></ul>
<dl><dd><ul><li> 拥有静态存储期且非临时的对象，或
</li><li> 拥有静态存储期的临时对象，但其值满足下文对纯右值的制约，或
</li><li> <span class="t-rev-inl t-since-cxx20"><span>非<a href="consteval.html" title="cpp/language/consteval">立即</a></span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>函数
</li></ul>
</dd></dl>
<ul><li> 其值满足下列制约的纯右值<i>核心常量表达式</i>：
</li></ul>
<dl><dd><ul><li> 若其值是类类型对象，则其每个引用类型的非静态数据成员均指代满足上述对泛左值制约的实体
</li><li> 若其值为指针类型，则它保有
</li></ul>
<dl><dd><ul><li> 拥有静态存储期的对象的地址
</li><li> 拥有静态存储期的对象的末尾后一位置的地址
</li><li> <span class="t-rev-inl t-since-cxx20"><span>非<a href="consteval.html" title="cpp/language/consteval">立即</a></span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>函数的地址
</li><li> 空指针值
</li></ul>
</dd></dl>
<ul><li> <span class="t-rev-inl t-since-cxx20"><span>若其值是成员函数指针类型，则它不代表<a href="consteval.html" title="cpp/language/consteval">立即函数</a></span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>
</li><li> 若其值具有类或数组类型，则每个子对象均满足这些对值的制约
</li></ul>
</dd></dl>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：列出要求非整数/经转换的常量表达式的语境？ </td></tr></table>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></td></tr>
<tr class="t-rev t-until-cxx14"><td>
<p><i>常量表达式</i>是字面常量表达式，引用常量表达式，或地址常量表达式。
</p><p><i>字面常量表达式</i>是非指针字面类型（经语境所要求的转换后）的纯右值<i>核心常量表达式</i>。数组或类类型的字面常量表达式要求每个子对象以常量表达式初始化。
</p><p><i>引用常量表达式</i>是指代具有静态存储期的对象或指代函数的左值核心常量表达式。
</p><p><i>地址常量表达式</i>是 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../types/nullptr_t.html"><span class="kw102">std::<span class="me2">nullptr_t</span></span></a></span></span> 类型或指针类型（经语境所要求的转换后）的纯右值核心常量表达式，它指向具有静态存储期的对象，指向具有静态存储期的数组末尾后一位置，指向函数，或为空指针。
</p>
</td>
<td><span class="t-mark-rev t-until-cxx14">(C++14 前)</span></td></tr>
</table>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> test<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> a <span class="sy1">=</span> <a href="../numeric/random/random_device.html"><span class="kw1486">std::<span class="me2">random_device</span></span></a><span class="br0">{</span><span class="br0">}</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">constexpr</span> <span class="kw4">const</span> <span class="kw4">int</span><span class="sy3">&amp;</span> ra <span class="sy1">=</span> a<span class="sy4">;</span> <span class="co1">// OK：a 是泛左值常量表达式</span>
    <span class="kw4">constexpr</span> <span class="kw4">int</span> ia <span class="sy1">=</span> a<span class="sy4">;</span> <span class="co1">// 错误：a 不是纯右值常量表达式</span>
 
    <span class="kw4">const</span> <span class="kw4">int</span> b <span class="sy1">=</span> <span class="nu0">42</span><span class="sy4">;</span>
    <span class="kw4">constexpr</span> <span class="kw4">const</span> <span class="kw4">int</span><span class="sy3">&amp;</span> rb <span class="sy1">=</span> b<span class="sy4">;</span> <span class="co1">// 错误：b 不是泛左值常量表达式</span>
    <span class="kw4">constexpr</span> <span class="kw4">int</span> ib <span class="sy1">=</span> b<span class="sy4">;</span> <span class="co1">// OK：b 是纯右值常量表达式</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">注解</span></h3>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<p>不容许实现将库函数声明为 <code>constexpr</code>，除非标准说该函数为 <code>constexpr</code>。
</p><p>常量表达式中不容许<a href="copy_elision.html" title="cpp/language/copy elision">具名返回值优化</a>（ NRVO ），而返回值优化（ RVO ）是强制要求的。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> 应用于
</th>
<th> 出版时的行为
</th>
<th> 正确行为
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue1313">CWG 1313</a>
</td>
<td> C++11
</td>
<td> 容许未定义行为，且禁止所有指针减法
</td>
<td> 同数组内的指针减法 OK ，禁止 UB
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue1952">CWG 1952</a>
</td>
<td> C++11
</td>
<td> 要求诊断标准库未定义行为
</td>
<td> 未指定是否诊断库 UB
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue2167">CWG 2167</a>
</td>
<td> C++14
</td>
<td> 求值中局部的非成员引用会令求值为非 constexpr
</td>
<td> 允许非成员引用
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue2299">CWG 2299</a>
</td>
<td> C++11
</td>
<td> <a href="../header/cstdarg.html" title="cpp/header/cstdarg"><tt>&lt;cstdarg&gt;</tt></a> 中的宏能否用于常量求值不明确
</td>
<td> 禁止 <code>va_arg</code> ，未指定 <code>va_start</code>
</td></tr></table>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td>  <a href="constexpr.html" title="cpp/language/constexpr"> <code>constexpr</code> 说明符</a>
</td>
<td> 说明变量或函数的值能在编译时计算<span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../types/is_literal_type.html" title="cpp/types/is literal type"> <span class="t-lines"><span>is_literal_type</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-deprecated-cxx17">(C++17 中弃用)</span></span><span><span class="t-mark-rev t-until-cxx20">(C++20 中移除)</span></span></span></div></div>
</td>
<td>  检查类型是否为字面类型 <br> <span class="t-mark">(类模板)</span> </td></tr>
</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 4358/1000000
Preprocessor generated node count: 12378/1000000
Post‐expand include size: 86733/2097152 bytes
Template argument size: 19870/2097152 bytes
Highest expansion depth: 22/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:8207-0!*!0!!zh!*!zh!* and timestamp 20200223060655 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/constant_expression&amp;oldid=64493">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/constant_expression&amp;oldid=64493</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/constant_expression">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2020年2月8日 (星期六) 00:03。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过9,610次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 1.556 secs. -->
	</body>
<!-- Cached 20200223060655 -->
</html>